package com.mars.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mars.ticket.common.constant.Constant;
import com.mars.ticket.entity.SysUser;
import com.mars.ticket.entity.User;
import com.mars.ticket.exception.WorkException;
import com.mars.ticket.mapper.SysUserMapper;
import com.mars.ticket.mapper.UserMapper;
import com.mars.ticket.request.ChatRecordRequest;
import com.mars.ticket.response.ChatRecordResponse;
import com.mars.ticket.response.UserResponse;
import com.mars.ticket.response.base.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mars.ticket.service.IChatRecordService;
import com.mars.ticket.mapper.ChatRecordMapper;
import com.mars.ticket.entity.ChatRecord;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


/**
 * ChatRecord 实现层
 *
 * @Author wq
 * @Date 2021-03-15
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class ChatRecordServiceImpl extends ServiceImpl<ChatRecordMapper, ChatRecord> implements IChatRecordService {

    @Resource
    private UserMapper userMapper;


    @Resource
    private SysUserMapper sysUserMapper;


    @Override
    public void send(ChatRecordRequest request) {
        if (StringUtils.isEmpty(request.getContent())) {
            throw new WorkException("发送消息不能为空");
        }
        if (Objects.isNull(request.getToUserId())) {
            throw new WorkException("请选择发送对象");
        }
        ChatRecord chatRecord = new ChatRecord();
        chatRecord.setCreateTime(new Date());
        chatRecord.setStatus(Constant.ZERO);
        chatRecord.setContent(request.getContent());
        chatRecord.setFromUserId(request.getFromUserId());
        chatRecord.setToUserId(request.getToUserId());
        save(chatRecord);
    }

    @Override
    public void reply(ChatRecordRequest request) {
        if (StringUtils.isEmpty(request.getContent())) {
            throw new WorkException("回复消息不能为空");
        }
        ChatRecord chatRecord = new ChatRecord();
        chatRecord.setCreateTime(new Date());
        chatRecord.setStatus(Constant.ONE);
        chatRecord.setContent(request.getContent());
        chatRecord.setFromUserId(request.getFromUserId());
        chatRecord.setToUserId(request.getToUserId());
        updateById(chatRecord);
    }

    @Override
    public PageInfo<ChatRecordResponse> receive(ChatRecordRequest request) {
        IPage<ChatRecord> page = new Page<>(request.getPageNumber(), request.getPageSize());
        LambdaQueryWrapper<ChatRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChatRecord::getToUserId, request.getToUserId());
        wrapper.orderByDesc(ChatRecord::getCreateTime);
        IPage<ChatRecord> chatRecordIPage = baseMapper.selectPage(page, wrapper);
        if (!CollectionUtils.isEmpty(chatRecordIPage.getRecords())) {
            List<ChatRecordResponse> collect = chatRecordIPage.getRecords().stream().map(x -> {
                ChatRecordResponse response = new ChatRecordResponse();
                response.setFromUserId(x.getFromUserId());
                User user = userMapper.selectById(x.getFromUserId());
                if (Objects.nonNull(user)) {
                    response.setFormUserNickname(user.getNickname());
                }
                response.setToUserId(x.getToUserId());

                response.setContent(x.getContent());
                response.setStatus(x.getStatus());
                response.setCreateTime(x.getCreateTime());
                response.setId(x.getId());
                return response;

            }).collect(Collectors.toList());
            return new PageInfo<>(page.getTotal(), collect);
        }
        return null;
    }

    @Override
    public void delete(Integer id) {
        baseMapper.deleteById(id);
    }

    @Override
    public List<UserResponse> chatList(Integer fromUserId) {

        return null;
    }
}